package com.warpcode.gpsloggerlite;

import java.util.Vector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.location.Location;

public class LocationDatabase extends SQLiteOpenHelper 
{
    private final static String DATABASE_NAME = "locationdb.db";
    private final static String TABLE_NAME = "location";
    private final static int VERSION = 6;
    private final static String COORDINATES_ID = "_id";
    private final static String TIME_COLUMN = "time";
    private final static String LATITUDE_COLUMN = "latitude";
    private final static String LONGITUDE_COLUMN = "longitude";
    private final static String ALTITUDE_COLUMN = "altitude";
    private final static String SPEED_COLUMN = "speed";
    
    private SQLiteDatabase db;
    
    public LocationDatabase(Context context) 
    {
    	super(context,DATABASE_NAME,null,VERSION);
    	db = getWritableDatabase();
    }
        
    @Override
    public void onCreate(SQLiteDatabase db) 
    {
    	db.execSQL("CREATE TABLE "+TABLE_NAME+"("
                                  +COORDINATES_ID+" INTEGER PRIMARY KEY, "
                                  +TIME_COLUMN+" TEXT, "
                                  +LATITUDE_COLUMN+" TEXT, "
                                  +LONGITUDE_COLUMN+" TEXT, "
                                  +ALTITUDE_COLUMN+" TEXT, "
                                  +SPEED_COLUMN+" TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
    	db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);        
    }
        
    public void addLocationAtTime(Location loc,android.text.format.Time t)
    {            
    	ContentValues values = new ContentValues();
        values.put(TIME_COLUMN,""+t.format("%d/%m/%Y  %H:%M:%S"));
        values.put(LATITUDE_COLUMN, loc.getLatitude());
        values.put(LONGITUDE_COLUMN, loc.getLongitude());
        values.put(ALTITUDE_COLUMN, loc.getAltitude());
        values.put(SPEED_COLUMN,loc.getSpeed());
                
        db.insert(TABLE_NAME, null, values);
    }
        
    public Cursor getLocations()
    {
    	return db.query(false, TABLE_NAME, null, null, null, null, null, null, null);
    }
        
    public String[] getLocationWithId(int id)
    {
    	Cursor cursor = db.query(false, TABLE_NAME, null,null,null,null,null,null,null);
        String loc[] = null;
        if(cursor.moveToFirst())
        {
        	do
            {
        		if(id==cursor.getInt(0))
                {
        			loc = new String[5];
                    loc[0] = cursor.getString(1);
                    loc[1] = cursor.getString(2);
                    loc[2] = cursor.getString(3);
                    loc[3] = cursor.getString(4);
                    loc[4] = cursor.getString(5);
                }
            }while(cursor.moveToNext());
        }
                
        if(!cursor.isClosed())
        {
        	cursor.close();
        }
        return loc;
    }
        
    public Vector<String[]> getLocationsMatrix()
    {
    	Cursor cursor = db.query(false, TABLE_NAME, null,null,null,null,null,null,null);
        Vector<String[]> loc = new Vector<String[]>();
        String l[] = null;
        if(cursor.moveToFirst())
        {
        	do
            {
        		l = new String[5];
                l[0] = cursor.getString(1);
                l[1] = cursor.getString(2);
                l[2] = cursor.getString(3);
                l[3] = cursor.getString(4);
                l[4] = cursor.getString(5);
                loc.add(l);
            }while(cursor.moveToNext());
         }
                
         if(!cursor.isClosed())
         {
        	 cursor.close();
         }       
         return loc;          
    }
        
    public void deleteAll()
    {
    	db.delete(TABLE_NAME, null, null);    
    }
        
    public void deleteWithId(int id)
    {
    	String where = "_id=?";
        String args[] = {""+id};
        db.delete(TABLE_NAME, where, args);
    }
        
    public boolean editWithId(int id,String vals[])
    {
    	ContentValues val = new ContentValues();
        val.put(LATITUDE_COLUMN, Float.parseFloat(vals[0]));
        val.put(LONGITUDE_COLUMN, Float.parseFloat(vals[1]));
        val.put(ALTITUDE_COLUMN, Float.parseFloat(vals[2]));
        val.put(SPEED_COLUMN, Float.parseFloat(vals[3]));

        String where = "_id=?";
        String args[] = {""+id};

        try
        {
        	db.update(TABLE_NAME,val,where,args);
            return true;
        }
        catch(Exception e)
        {
        	return false; 
        }
    }
}